#include "field.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


void field_write(const Field  *field1,
				 const Field  *field2,
				 const Field  *field3,
				 const Field  *field4,
				 const Field  *field5,
				 const Field  *field6,
				 const Field  *field7,
                 const Field  *field8,
				 const Field  *field9,
				 const Field  *field10,
				 const int     cycle,
                 const double  dx,
                 const double  dy)
{
    CHECK_FIELD(field1);
	CHECK_FIELD(field2);
	CHECK_FIELD(field3);
	CHECK_FIELD(field4);
	CHECK_FIELD(field5);
	CHECK_FIELD(field6);
	CHECK_FIELD(field7);
	CHECK_FIELD(field8);
	CHECK_FIELD(field9);
	CHECK_FIELD(field10);
	CHECK_FIELDS(field1, field2);
	CHECK_FIELDS(field1, field3);
	CHECK_FIELDS(field1, field4);
	CHECK_FIELDS(field1, field5);
	CHECK_FIELDS(field1, field6);
	CHECK_FIELDS(field1, field7);
	CHECK_FIELDS(field1, field8);
	CHECK_FIELDS(field1, field9);
	CHECK_FIELDS(field1, field10);
    {
        int             i,j;
        const int       nx    = field1->nx;
        const int       ny    = field1->ny;
        const double ** rhou = CONST_ARRAY_OF(field1);
		const double ** rhov = CONST_ARRAY_OF(field2);
		const double ** rho = CONST_ARRAY_OF(field3);
		const double ** sig11 = CONST_ARRAY_OF(field4);
		const double ** sig12 = CONST_ARRAY_OF(field5);
		const double ** sig22 = CONST_ARRAY_OF(field6);
		const double ** P     = CONST_ARRAY_OF(field7);
		const double ** dvg   = CONST_ARRAY_OF(field8);
		const double ** eta1  = CONST_ARRAY_OF(field9);
		const double ** zeta1 = CONST_ARRAY_OF(field10);
        
        FILE *          fp;
		 char filename[1000]; 
		sprintf(filename,"temp/%d.vtk",cycle);
		fp    = fopen(filename, "wt");
        
        if( NULL == fp )
        {
            perror(filename);
            exit(1);
        }
		fprintf(fp,"# vtk DataFile Version 3.0\n# Exemple STRUCTURED_POINTS\nASCII\nDATASET STRUCTURED_POINTS\nDIMENSIONS %d %d 1 \n ORIGIN 0 0 0\nSPACING %g\t%g\t%g\n ASPECT_RATIO 1 1 1\n POINT_DATA %d\n",nx,ny,dx,dy,0.0,nx*ny);
		
		// pour chaque pas de tps on cree un fichier dans le quel on met les valeur de la position et de la vitesse
		
		fprintf(fp,"SCALARS rho double\nLOOKUP_TABLE defaut\n");
		for(j=0;j<ny;j++)
		{
			for(i=0;i<nx;i++)
			{
				fprintf(fp,"%le\n" , rho[j][i]);
			}
		}	
		fprintf(fp,"SCALARS P double\nLOOKUP_TABLE defaut\n");
		for(j=0;j<ny;j++)
		{
			for(i=0;i<nx;i++)
			{
				fprintf(fp,"%le\n" , P[j][i]);
			}
		}
		fprintf(fp,"SCALARS sigma11 double\nLOOKUP_TABLE defaut\n");
		for(j=0;j<ny;j++)
		{
			for(i=0;i<nx;i++)
			{
				fprintf(fp,"%le\n" , sig11[j][i]);
			}
		}
		
		fprintf(fp,"SCALARS sigma12 double\nLOOKUP_TABLE defaut\n");
		for(j=0;j<ny;j++)
		{
			for(i=0;i<nx;i++)
			{
				fprintf(fp,"%le\n" , sig12[j][i]);
			}
		}
		
		fprintf(fp,"SCALARS sigma22 double\nLOOKUP_TABLE defaut\n");
		for(j=0;j<ny;j++)
		{
			for(i=0;i<nx;i++)
			{
				fprintf(fp,"%le\n" , sig22[j][i]);
			}
		}
		
		fprintf(fp,"SCALARS eta double\nLOOKUP_TABLE defaut\n");
		for(j=0;j<ny;j++)
		{
			for(i=0;i<nx;i++)
			{
				fprintf(fp,"%le\n" , eta1[j][i]);
			}
		}
		
		fprintf(fp,"SCALARS zeta double\nLOOKUP_TABLE defaut\n");
		for(j=0;j<ny;j++)
		{
			for(i=0;i<nx;i++)
			{
				fprintf(fp,"%le\n" , zeta1[j][i]);
			}
		}
		
		fprintf(fp,"VECTORS U double\n");
		for(j=0;j<ny;j++)
		{
			for(i=0;i<nx;i++)
			{
				//fprintf(fp,"%le\t%le\t 0\n" ,array1[j][i] ,array2[j][i]);
				fprintf(fp,"%le\t%le\t%le\n" ,rhou[j][i] ,rhov[j][i],0.0);
			}
		}
        
        fprintf(fp,"SCALARS dvg double\nLOOKUP_TABLE defaut\n");
		for(j=0;j<ny;j++)
		{
			for(i=0;i<nx;i++)
			{
				fprintf(fp,"%le\n" , dvg[j][i]);
			}
		}
        
       fclose(fp);
    }
    
}

/*#include <string.h>

void field_save(const Field *f,
				const char  *prefix,
                const int    cycle,
                const double dx,
                const double dy)
{
    CHECK_FIELD(f);
    assert(prefix);
    {
        const int maxlen   = strlen(prefix) + 32;
        char     *filename = (char *)calloc( maxlen, 1);
        if( NULL == filename )
        {
            fprintf( stderr, "can't allocate filename");
            exit(1);
        }
        snprintf( filename, maxlen, "%s-%d.vti", prefix, cycle);
        field_write(f, g, h,dx,dy);
        free(filename);
    }
    
    
}*/
